package cn.tedu.v1.service.impl;

import cn.tedu.v1.exception.ServiceException;
import cn.tedu.v1.mapper.PrescriptionMapper;
import cn.tedu.v1.mapper.PrescriptionSumMapper;
import cn.tedu.v1.model.Prescription;
import cn.tedu.v1.model.PrescriptionSum;
import cn.tedu.v1.service.IPrescriptionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 处方 服务实现类
 * </p>
 *
 * @author tedu.cn
 * @since 2021-07-06
 */
@Service
public class PrescriptionServiceImpl extends ServiceImpl<PrescriptionMapper, Prescription> implements IPrescriptionService {

    @Autowired
    PrescriptionMapper prescriptionMapper;
    @Override
    public void submitPrescription(Prescription prescription) {
        prescriptionMapper.insert(prescription);
    }

    @Autowired
    PrescriptionSumMapper prescriptionSumMapper;
    @Override
    public List<Prescription> showAll() {
        List<Prescription> prescriptionList=prescriptionMapper.selectList(null);
        for(Prescription p:prescriptionList){
            List<PrescriptionSum> prescriptionSums=prescriptionSumMapper.selectBetween(p.getStart(),p.getEnd());
            p.setPrescriptionSums(prescriptionSums);
        }
        return prescriptionList;
    }

    @Override
    public Prescription showPrescriptionById(Integer id) {
        Prescription p=prescriptionMapper.selectById(id);
        p.setPrescriptionSums(prescriptionSumMapper.selectBetween(p.getStart(),p.getEnd()));
        return p;
    }

    @Override
    public void pay(Integer prescriptionId) {
        int num= prescriptionMapper.pay(prescriptionId);
        if(num!=1){
            throw new ServiceException("支付失败");
        }
    }
}
